package config

import (
	"gopkg.in/yaml.v2"
	"io/ioutil"
)

//服务端配置
type appConfig struct {
	Port       string   `yaml:"port"`
	JwtKey     string   `yaml:"jwyKey"`
	ExpireTime int      `yaml:"expireTime"`
	IgnoreUrls []string `yaml:"ignoreUrls"`

	DataBase   dataBase `yaml:"MySql"`
	Redis      redis    `yaml:"Redis"`
}

/**
 * mysql配置
 */
type dataBase struct {
	Host         string `yaml:"host"`
	Port         string `yaml:"port"`
	User         string `yaml:"user"`
	Pwd          string `yaml:"pwd"`
	Database     string `yaml:"database"`
	MaxOpenConns int    `yaml:"maxOpenConns"`
	MaxIdleConns int    `yaml:"maxIdleConns"`
	ShowSql      bool   `yaml:"showSql"`
}

/**
 * Redis 配置
 */
type redis struct {
	Addr     string `yaml:"addr"`
	Port     string `yaml:"port"`
	Password string `yaml:"passWord"`
}

var config *appConfig

func AppConfig() *appConfig {
	return config
}

//初始化服务器配置
func InitConfig() *appConfig {
	file, err := ioutil.ReadFile("./config.yaml")
	if err != nil {
		panic(err.Error())
	}

	err = yaml.Unmarshal(file, &config)
	if err != nil {
		panic(err.Error())
	}
	return config
}
